package com.saas.tenant.tenantConfig;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @auther Jiang Tian
 * @date 2025/2/27
 * @apiNote
 **/
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(MyTenantLineHandler myTenantLineHandler){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //多租户插件
        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
        tenantLineInnerInterceptor.setTenantLineHandler(myTenantLineHandler);

        interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        return interceptor;
    }

//    @Bean
//    public MybatisPlusInterceptor mybatisPlusInterceptor() {
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//
//        if (tenantProperties.getEnable()) {
//            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
//                @Override
//                public Expression getTenantId() {
//
//                    return new StringValue(TenantContextHolder.getTenantId());
//                }
//
//                /**
//                 * 该方法默认返回 false 表示所有表都需要拼多租户条件
//                 * 过滤不需要区分多租户的表
//                 * @param tableName
//                 * @return
//                 */
//                @Override
//                public boolean ignoreTable(String tableName) {
//                    return tenantProperties.getExclusionTable().stream().anyMatch(t -> t.equalsIgnoreCase(tableName));
//                }
//
//                /**
//                 * 获取多租户的字段名
//                 * @return
//                 */
//                @Override
//                public String getTenantIdColumn() {
//                    return tenantProperties.getColumn();
//                }
//            }));
//        }
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//        return interceptor;
//    }
}
